﻿using System;
using System.Collections.Generic;
using System.Text;
using Store.Model.Models;
using Store.Dal;
using Store.ViewModel;
using StackExchange.Redis;
using Store.Helper;
using Newtonsoft.Json;

namespace Store.Bll
{
    public class HandlingOrderBll
    {
        //实例化dal
        private readonly HandlingOrderDal dal;
        private readonly IDatabase _redis;
        public HandlingOrderBll(HandlingOrderDal _dal, RedisHelper client)
        {
            dal = _dal;
            _redis = client.GetDatabase();
        }

        //显示查询
        public List<OrderView> GetOrders(int sort,int state, string number, string user,
            string time, int page, int limit, out int total)
        {
            var data = dal.GetOrders(sort,state, number, user, time, page, limit, out total);
            string json = JsonConvert.SerializeObject(data);
            _redis.StringSet("data", json);
            if (_redis.IsConnected("data"))
            {
                string a = _redis.StringGet("data");
                return JsonConvert.DeserializeObject<List<OrderView>>(a);
            }
            else
            {
                return data;
            }
            
        }

        //显示订单信息
        public List<OrderInfo> GetOrder(int id)
        {
            return dal.GetOrder(id);
        }

        //显示收货人信息
        public List<UserInfo> GetUserInfo(int id)
        {
            return dal.GetUserInfo(id);
        }

        //显示商品信息
        public List<CommodityInfo> GetCommodity(int id)
        {
            return dal.GetCommodity(id);
        }

        //显示费用合计
        public List<CostView> GetCostViews(int id)
        {
            return dal.GetCostViews(id);
        }

        //修改
        public int UpdateUser(int userid, string username, string userphone, string addressname, string addresspostal)//User user,Addresse addresse
        {
            return dal.UpdateUser(userid,username,userphone,addressname,addresspostal);
            //dal.UpdateUserAddress(addresse);
            //return 1;
        }
        //修改用户地址和邮政编码
        //public int UpdateUserAddress(Addresse addresse)
        //{
        //    return dal.UpdateUserAddress(addresse);
        //}

        //添加订单
        public int AddOrder(Model.Models.Order orders)
        {
            return dal.AddOrder(orders);
        }

        //修改订单状态
        public int UpdOrderState(int id, int state)
        {
            return dal.UpdOrderState(id, state);
        }

        //批量修改状态
        public int UpdOrderStates(string[] ids)
        {
            return dal.UpdOrderStates(ids);
        }
    }
}
